package com.android.clinic.doctors.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.base.BaseActivity;
import com.android.clinic.doctors.app.entity.UserInfo;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.ui.main.MainActivity;
import com.android.clinic.doctors.app.utils.Constants;
import com.android.clinic.doctors.app.utils.PreferencesUtils;
import com.android.clinic.doctors.app.utils.Utils;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 登录页面
 * Created by cheng on 2018/6/12.
 */
public class LoginActivity extends BaseActivity {

    private EditText etCardNo, etPassword;
    private String cardNo = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.setVisibility(View.GONE);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        etCardNo = findViewById(R.id.et_card_no);
        etPassword = findViewById(R.id.et_password);
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        if (v.getId() == R.id.btn_login) {
            login();
        } else if (v.getId() == R.id.layout_protocol) {
            startActivity(new Intent(this, WebViewActivity.class).putExtra("path", Constants.PROTOCOL_URL));
        }
    }

    /**
     * 登录
     */
    private void login() {
        cardNo = etCardNo.getText().toString().trim();
        String password = etPassword.getText().toString().trim().toUpperCase();
        if (Utils.isEmpty(cardNo)) {
            showToast("卡号不能为空!");
            return;
        } else if (Utils.isEmpty(password)) {
            showToast("密码不能为空!");
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("doctorId", "");
            jsonObject.put("id", 0);
            jsonObject.put("memberId", cardNo);
            jsonObject.put("mobile", "");
            jsonObject.put("password", password);
            jsonObject.put("status", "");

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());
            HttpClient.getHttpService().login(body)
                    .compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers(loading, "登录中..."))
                    .subscribe(new BaseObserver<UserInfo>() {
                        @Override
                        public void onSuccess(UserInfo userInfo) {
                            PreferencesUtils.putString(LoginActivity.this, Constants.CARD_NO, cardNo);
                            ClinicDoctorsApplication.saveUserInfo(userInfo);
                            angoraLogin(cardNo);
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
